﻿using System.Web.Mvc;
using GdNet.Web.Mvc;
using GdNet.WebFramework.Business.System;
using GdNet.WebFramework.Modules.Common;
using GdNet.WebFramework.Modules.Common.Assistants;
using GdNet.WebFramework.Modules.Common.ModuleBase;

namespace GdNet.WebFramework.Gui.Controllers
{
    public class AdminController : AbstractController
    {
        private ActionDetail _actionDetail;
        private IModuleActionService _actionService;
        private PageDetailModel _pageDetailModel;
        private readonly IModuleService _moduleService;

        public AdminController(IModuleService moduleService)
        {
            _moduleService = moduleService;
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            _actionDetail = ActionAssistant.Parse(base.Request);
            var moduleAction = _moduleService.GetModuleAction(_actionDetail.ModuleCode, _actionDetail.ActionCode);

            _actionService = ActionAssistant.LoadActionService(moduleAction);
            _pageDetailModel = new PageDetailModel()
            {
                Title = moduleAction.Name,
                Header = moduleAction.Name,
                PartialView = _actionService.GetViewPath(_actionDetail.ModuleCode, moduleAction.ViewName),
            };
        }

        public ActionResult Index()
        {
            _pageDetailModel.Result = _actionService.Execute(_actionDetail);
            return View(_pageDetailModel);
        }

        [HttpPost]
        public ActionResult Index(FormCollection formCollection)
        {
            ((IModuleActionPostbackService)_actionService).Submit(formCollection);
            return View(_pageDetailModel);
        }
    }
}
